package com.llxz.blog.article.controller;

import com.llxz.blog.article.dto.*;
import com.llxz.blog.article.entity.Article;
import com.llxz.blog.article.entity.Comment;
import com.llxz.blog.article.entity.UserInfo;
import com.llxz.blog.article.repository.ArticleRepository;
import com.llxz.blog.article.service.ArticleService;
import com.llxz.blog.article.util.JsonResult;
import com.llxz.blog.article.util.PageHelper;
import com.llxz.blog.article.util.TimeStamp;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author lyq && zzh
 * @description:. date: 2021/9/7 10:14
 */

@Api()
@RestController
@RequestMapping("/article")
@PropertySource("classpath:application.yml")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleRepository articleRepository;

    @Value("${server.port}")
    private String port;

    @PostMapping("/test")
    public JsonResult test() {
        return new JsonResult();
    }

    @GetMapping("get/total/count")
    public JsonResult getTotalCount() {
        Integer total = articleService.selectTotalCount();
        System.out.println(total + "============");
        return new JsonResult(total);
    }


    @PostMapping("get/my/blog")
    public JsonResult getMyBlog(@RequestBody CollectParam param,@PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> list = articleService.getBlogByPhone(param.getPhone());
        Page<Article> pages = new PageHelper().listConvertToPage(list, pageable);
        return new JsonResult(pages);
    }

    /**
     * 发布
     * @param article
     * @return
     */
    @PostMapping(value = "/public")
    public JsonResult addOneArticle(@RequestBody Article article) {
        if (article.getPhone() == null || "".equals(article.getPhone())) {
            return new JsonResult(2001,"请先登录");
        }
        List<UserInfo> likes = new ArrayList<>();
        List<Comment> comments = new ArrayList<>();
        article.setArticleId(article.getPhone() + new TimeStamp().getTimeStamp());
        article.setDate(new Date());
        article.setLikes(likes);
        article.setComments(comments);
        articleService.addOneArticle(article);
        return new JsonResult(article);
    }

    /**
     * 所有文章
     * @param pageable
     * @return
     */
    @PostMapping(value = "all")
    public JsonResult getAllArticleByDate(@PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> list = articleService.getAllArticleByDate();
        Page<Article> pages = new PageHelper().listConvertToPage(list, pageable);
        return new JsonResult(pages);
    }

    /**
     * 删除
     * @param article
     * @return
     */
    @PostMapping(value = "/delete")
    public JsonResult deleteOneArticle(@RequestBody Article article) {
        System.out.println("============"+article+" =====");
        articleService.deleteArticle(article);
        return new JsonResult();
    }

    /**
     * 更新
     * @param article
     * @return
     */
    @PostMapping(value = "/update")
    public JsonResult updateOneArticle(@RequestBody Article article) {
        System.out.println("update ================="+article);
        articleService.updateArticle(article);
        return new JsonResult();
    }
    /**
     * 我的文章
//     * @param article
     * @return
     */
//    @PostMapping(value = "/update")
//    public JsonResult updateOneArticle(String moblie) {
////        articleService.updateArticle(article);
////        return new JsonResult();
//    }

    /**
     *
     * @param article
     * @param pageable
     * @return
     */
    @PostMapping(value = "/select/labels")
    public JsonResult selectByLabels(@RequestBody Article article, @PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> lists =  articleService.selectArticleByLabel(article);
        Page<Article> pages = new PageHelper().listConvertToPage(lists, pageable);
        return new JsonResult(pages);
    }


    @GetMapping(value = "/select/hotlabels")
    public JsonResult selectHotLabels() {
        return new JsonResult(articleService.selectHotLabels());
    }




    /**
     * 评论
     * @param commentParams
     * @return
     */
    @PostMapping(value = "/comment/commit")
    public JsonResult commitComment(@RequestBody CommitCommentParams commentParams) {
        articleService.commitComment(commentParams);
        return new JsonResult();
    }

    /**
     * 删除评论
     * @param deleteCommentParams
     * @return
     */
    @PostMapping(value = "/comment/delete")
    public JsonResult deleteComment(@RequestBody DeleteCommentParams deleteCommentParams) {
        articleService.deleteMyComment(deleteCommentParams);
        return new JsonResult();
    }

    /**
     * 点赞
     * @param clickLikeParams
     * @return
     */
    @PostMapping(value = "/click/like")
    public JsonResult clickLike(@RequestBody ClickLikeParams clickLikeParams) {
        System.out.println("点赞");
        articleService.clickLike(clickLikeParams);
        return new JsonResult();
    }

    /**
     * 取消点赞
     * @param clickLikeParams
     * @return
     */
    @PostMapping(value = "/click/unlike")
    public JsonResult clickUnlike(@RequestBody ClickLikeParams clickLikeParams) {
        articleService.clickUnlike(clickLikeParams);
        return new JsonResult();
    }

    /**
     *
     * @param article
     * @return
     */
    @PostMapping(value = "/get/one")
    public  JsonResult SelectOneByArticleId(@RequestBody  Article article) {
        Article res = articleRepository.findByArticleId(article.getArticleId());
        return new JsonResult(res);
    }

    /**
     *
     * @param articleId
     * @return
     */
    @RequestMapping(value = "getOne/{articleId}", method = RequestMethod.GET)
    public JsonResult getOneByArticleId(@PathVariable String articleId) {
        Article temp = new Article();
        temp.setArticleId(articleId);
        Article article = articleService.selectOneByArticleId(temp);
        return new JsonResult(article);
    }

    /**
     *
     * @param pageable
     * @return
     */
    @PostMapping(value = "/get/type/{type}")
    public JsonResult getArticlesByType(@PathVariable(value = "type") String type, @PageableDefault(page = 0, size = 15) Pageable pageable) {
        System.out.println(type + "=================");
        List<Article> list = articleService.getArticlesByType(type);
        Page<Article> pages = new PageHelper().listConvertToPage(list, pageable);
        System.out.println(pages.getContent().size());
        return new JsonResult(pages);
    }

    @PostMapping(value = "/get/title")
    public JsonResult getArticlesByTitle(@RequestBody Article article, @PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> list = articleService.getArticlesByTitle(article);
        Page<Article> pages= new PageHelper().listConvertToPage(list, pageable);
        return new JsonResult(pages);
    }

    @PostMapping(value = "/get/share")
    public JsonResult getShareLink(@RequestBody Article article) {
        String url = "http://" + "172.29.3.123:8223:" + "/article/getOne/" + article.getArticleId();
        ShareLinkParams link = new ShareLinkParams();
        link.setLink(url);
        return new JsonResult(link);
    }

    @PostMapping(value = "/mine/articles")
    public JsonResult getMyArticles(@RequestBody Article article,  @PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> list = articleService.getMyArticles(article);
        Page<Article> pages = new PageHelper().listConvertToPage(list, pageable);
        return new JsonResult(pages);
    }

    @PostMapping(value = "/comment/reply")
    public JsonResult replyCommit(@RequestBody ReplyCommentParams replyCommentParams) {
        articleService.replyComment(replyCommentParams);
        return new JsonResult();
    }

    /**
     *删除评论
     * @param deleteReplyParams
     * @return
     */
    @PostMapping(value = "/reply/delete")
    public JsonResult deleteMyReply(@RequestBody DeleteReplyParams deleteReplyParams) {
        articleService.deleteReply(deleteReplyParams);
        return new JsonResult();
    }

    /**
     * 按热度获取
     * @return
     */
    @PostMapping(value = "/get/hot")
    public JsonResult getAllByVisit(@PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> articles = articleService.getHotArticles();
        Page<Article> pages = new PageHelper().listConvertToPage(articles, pageable);
//        for (Article page : pages) {
//            System.out.println("返回的文章为："+page);
//        }
        return new JsonResult(pages);
    }

    /**
     * 删除某人所有（Admin）
     * @param phone
     * @return
     */
    @PostMapping(value = "/delete/ones/{phone}")
    public int deleteOnesArticle(@PathVariable String phone) {
        int flag = articleService.deleteOnesAll(phone);
        return flag;
    }


    /**
     * （Admin）未通过文章
     * @param pageable
     * @return
     */
    @PostMapping(value = "/notPass")
    public JsonResult getNotPassArticle(@PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> list = articleService.getNotPassArticles();
        Page<Article> pages = new PageHelper().listConvertToPage(list, pageable);
        return new JsonResult(pages);
    }

    /**
     * pass （Admin）
     * @param articleId
     * @return
     */
    @PostMapping(value = "/check/pass/{articleId}")
    public JsonResult checkAndPass(@PathVariable String articleId) {
        articleService.CheckAndPass(articleId);
        return new JsonResult();
    }

    /**
     * reject （Admin）
     * @param articleId
     * @return
     */
    @PostMapping(value = "/check/reject/{articleId}")
    public JsonResult checkAndReject(@PathVariable String articleId) {
        articleService.CheckAndReject(articleId);
        return new JsonResult();
    }

    /**
     * 根据日期统计(Admin)
     * @param dateStr
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "get/day/{dateStr}")
    public int getEveryDayArticles(@PathVariable String dateStr) {
        int sum = articleService.getDayCount(dateStr);
        return sum;
    }

    /**
     * 判断是否点赞
     * @param collectParam
     * @return
     */
    @PostMapping(value = "/like/if")
    public boolean isLike(@RequestBody CollectParam collectParam) {
        boolean flag = articleService.isLike(collectParam);
        return flag;
    }

    /**
     *
     * @param pageable
     * @return
     */
    @PostMapping(value = "/get/comment")
    public JsonResult getArticlesByComment(@PageableDefault(page = 0, size = 15) Pageable pageable) {
        List<Article> articles = articleService.getArticlesByComment();
        Page<Article> pages = new PageHelper().listConvertToPage(articles, pageable);
        return new JsonResult(pages);
    }
}